package com.easyboot.web.system.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easyboot.utils.CommonUtil;
import com.easyboot.framework.domain.DataTableDto;
import com.easyboot.web.system.domain.SysLog;
import com.easyboot.web.system.mapper.SysLogMapper;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class SysLogService extends ServiceImpl<SysLogMapper, SysLog>{
    @Autowired
    private SysLogMapper sysLogMapper;
    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     *
     * @author yushuo
     * @description //角色列表
     * @date 16:38 2021/2/1
     * @param [dto, roleName, roleCode]
     * @return com.easyboot.framework.domain.DataTableDto
     **/
    public DataTableDto getList(DataTableDto dto,String logTitle, int logType, Date logDate){
        LambdaQueryWrapper<SysLog> query=new LambdaQueryWrapper<>();
        if(logType>0){
            query.eq(SysLog::getLogType,logType);
        }
        if(logDate!=null){
            query.ge(SysLog::getLogTime,logDate);
        }
        if(StrUtil.isNotEmpty(logTitle)){
            query.eq(SysLog::getLogTitle,logTitle);
        }
        query.orderByDesc(SysLog::getLogTime) ;
        Page<SysLog> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        IPage<SysLog> userIPage = sysLogMapper.selectPage(page, query);
        dto.setTotal(userIPage.getTotal());
        dto.setRows(userIPage.getRecords());
        return dto;
    }



    /**
    *
     * @author yushuo
     * @description 批量写日志 两者插入方法 1
     * @date 13:49 2021/6/25
     * @param [logList]
     * @return void
    **/
    public void batchSaveLogTrans(List<SysLog> logList) {

        SqlSession sqlsession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        SysLogMapper logMapper = sqlsession.getMapper(SysLogMapper.class);
        for (SysLog log : logList) {
            sysLogMapper.insert(log);
        }
        sqlsession.commit();
        sqlsession.clearCache();
        sqlsession.close();
    }

    /**
    *
     * @author yushuo
     * @description 批量保存log 两者插入方法 2
     * @date 14:28 2021/6/25
     * @param [logList]
     * @return void
    **/
    public void batchSaveLog(List<SysLog> logList) {

          saveBatch(logList,300);
    }
    /**
    *
     * @author yushuo
     * @description 批量删除
     * @date 13:26 2021/6/25
     * @param [ids]
     * @return void
    **/
    public void removeAll(String ids){
        List<Integer> idsArray = CommonUtil.toIntList(ids);

        sysLogMapper.deleteBatchIds(idsArray);

    }

    /**
     * @author yushuo
     * @description 清空表
     * @date 13:27 2021/6/25
     * @param []
     * @return void
    **/
    public void clearAll(){
        //remove(new QueryWrapper<>());
        sysLogMapper.clearSysLog();
    }

}




